/*
 
 OC 和 Swift 混编, swift类只能继承 NSObject (或者是 swift 分类) ,在 OC 中才能使用 swift 类 ,如果继承自 其他,则不可用,编译会报错.
 
 
 
 图片文件夹
 
 icon_black_scancode.imageset :黑二维码
 
 icon_black_scancode.imageset :二维码
 scanicon.imageset :二维码
 
 
 icon_search.imageset :搜索
 icon_zfb_gray.imageset 支付宝
 icon_qq.imageset  qq
 v2_about_wechat_logo.imageset 微信
 
 /Users/huangyongfeng/Desktop/Orchard/Orchard/Orchard/EGHomeViewController.m:26:55: Property follows Cocoa naming convention for returning 'owned' objects
 
 */

#import "EGHomeViewController.h"
#import "NetworkTool.h"
#import "EGHomeAdCell.h"
#import "EGAdView.h"
#import "EGHomeModel.h"
#import "EGWebViewController.h"
#import "EGNewYearCell.h"
#import "EGTwodemensionController.h"
#import "EGSearchController.h"
#import "EGFreshModel.h"
#import "EGAdViewCell.h"
#import "EGGoodsDetailController.h"



@interface EGHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) UICollectionView *collectionView;

@property (strong,nonatomic) UIPageControl *pageControl;

@property (strong,nonatomic) NSTimer *timer;

@property (strong,nonatomic) NSArray <EGHomeModel *> *adList;

@property (strong,nonatomic) NSArray <EGHomeModel *> *yearArray;

@property (strong,nonatomic) NSArray <EGFreshModel *> *freshArray;

@property (strong,nonatomic) NSArray <EGHomeModel *> *iconsArray;

@property (strong,nonatomic) EGAdView *adView;

//tableView
@property (strong,nonatomic) UITableView *newYearTableView;

@property (strong,nonatomic) UIView *headerView;

@property (strong,nonatomic) NSMutableArray *mArray2;


@end

@implementation EGHomeViewController


#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //防止产生内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupUI];
    [self loadAdData];
    [self loadIconsData];
    [self loadActivitiesData];
    [self loadFreshSellData];
    [self setupTimer];
    
    //跳转隐藏tabBar
    self.hidesBottomBarWhenPushed = YES;
    
}


#pragma mark - 添加定时器
- (void)setupTimer{
    
    
    self.timer = [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)nextPage{
    
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + SCREEN_WIDTH, 0) animated:YES];
}


#pragma mark -  当滚动到第二组第一页的时候,悄悄的滚回来
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    //求出 index
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    index = index %self.adList.count;
    
    self.pageControl.currentPage = index;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:1] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

#pragma mark - 手动滚动停止的时候.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    index = index %self.adList.count;
    
    self.pageControl.currentPage = index;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:1] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
}

#pragma mark - 手动开始拖拽,停止计时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
}

#pragma mark - 手动拖拽停止的时候,开启定时器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self setupTimer];
}


#pragma mark - 刷新数据
- (void)setAdList:(NSArray<EGHomeModel *> *)adList{
    
    _adList = adList;
    //刷新UI
    [self.collectionView reloadData];
}

#pragma mark - 刷新数据
- (void)setFreshArray:(NSArray<EGFreshModel *> *)freshArray{
    
    _freshArray = freshArray;
    
    self.mArray2 = [NSMutableArray array];
    
    for (int i = 0; i < freshArray.count; i++) {
        
        NSMutableArray *mArray1 = [NSMutableArray array];
        
        if (i < freshArray.count - 1) {
            
            [mArray1 addObject:freshArray[i]];
            [mArray1 addObject:freshArray[i + 1]];
        }
        
        i++;
        
        [self.mArray2 addObject:mArray1];
    }
    
    [self.newYearTableView reloadData];
}

#pragma mark - 获取新鲜热卖数据
- (void)loadFreshSellData{
    
    [EGFreshModel getFreshSellDataWithSuccess:^(NSArray *freshArray) {
        
        self.freshArray = freshArray;
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 刷新数据
- (void)setIconsArray:(NSArray<EGHomeModel *> *)iconsArray{
    
    _iconsArray = iconsArray;
    
    [self.newYearTableView reloadData];
}

#pragma mark - 获取 adView 的数据
- (void)loadIconsData{
    
    [EGHomeModel getIconDataWithSuccess:^(NSArray *iconsArray) {
        
       self.iconsArray = iconsArray;
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 刷新数据
- (void)setYearArray:(NSArray<EGHomeModel *> *)yearArray{
    
    _yearArray = yearArray;

    //刷新数据
    [self.newYearTableView reloadData];
}


#pragma mark - 获取年货数据
- (void)loadActivitiesData{
    
    [EGHomeModel getActivitiesDataWithSuccess:^(NSArray *activitiesArray) {
        
        self.yearArray = activitiesArray;
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 获取 collectionView 数据
- (void)loadAdData{
    
    [EGHomeModel getAdDataWithSuccess:^(NSArray *focusArray) {
        
        self.adList = focusArray;
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


#pragma mark - 刷新UI
- (void)setupUI{

    __weak typeof(self) weakSelf = self;
    
    
    [self.view addSubview:self.newYearTableView];
    
    [self.headerView addSubview:self.collectionView];
    [self.headerView addSubview:self.pageControl];
    
    
    [self.pageControl makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.headerView).offset(-10);
        make.bottom.equalTo(self.headerView).offset(10);
    }];

    
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(UIEdgeInsetsZero);
        
    }];

    NSArray *activitiesArray = @[@"抽奖",@"秒杀",@"抢红包",@"蜂暴团"];
    
    self.adView.adViewBlock = ^(NSString *title){
        
        for (int i = 0; i < activitiesArray.count ; i++) {
            
            if ([title isEqualToString:activitiesArray[i]]) {
                
                [weakSelf pushToWeb:weakSelf.adView.dataArray[i]];
                
            }
        }
    };
    

    [self.newYearTableView makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        
        make.bottom.equalTo(self.view).offset(-44);
        
    }];
}


#pragma mark -  push 到 webViewController
- (void)pushToWeb:(EGHomeModel *)model{
    
    EGWebViewController *webVc = [[EGWebViewController alloc]init];
    
    webVc.model = model;
    
    [self.navigationController pushViewController:webVc animated:YES];
}


#pragma mark -  collectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.adList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    EGHomeAdCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeAdCell" forIndexPath:indexPath];
    
    cell.model = self.adList[indexPath.item];
    
    return cell;
}

#pragma mark - tabelViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}


#pragma mark - numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
        
    }else if (section == 1){
        
        return self.yearArray.count;
        
    }else if(section == 2) {
        
        return self.freshArray.count/2;
    }
    
    return 0;
}


#pragma mark - cellForRow
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EGNewYearCell *cell;
    
    if (indexPath.section == 1) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"yearCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        EGHomeModel  *model = self.yearArray[indexPath.row];
        
        cell.homeModel = model;
        
    }else if (indexPath.section == 2){
        
        
        __weak typeof (self) weakSelf = self;
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"freshCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.freshArray =  self.mArray2[indexPath.row];
        
        cell.animationBlcok = ^(UIImageView *imageView){
            
             // MARK:调用动画方法
            [weakSelf addProductsAnimationWithImageView:imageView];
        };
        
        cell.cellBlock = ^(NSArray <EGFreshModel *> *freshArray,NSInteger index){
          
            EGGoodsDetailController *goodsVc = [[EGGoodsDetailController alloc]init];
            
            goodsVc.index = index;
            goodsVc.freshArray = freshArray;
            
            [self.navigationController pushViewController:goodsVc animated:YES];
        };
        
    }else if (indexPath.section == 0){
        
        EGAdViewCell *adViewCell = [tableView dequeueReusableCellWithIdentifier:@"activitiesCell" forIndexPath:indexPath];
        
        adViewCell.dataArray = self.iconsArray;
        
        adViewCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return adViewCell;
    }
    
    return cell;
}


#pragma mark - 选中cell --> 网页跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    if (indexPath.section == 0) {
        
        EGHomeModel *model = self.yearArray[indexPath.row];
        
        [self pushToWeb:model];
    }
}

#pragma mark - 动态设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1) {
        
        return 140;
        
    }else if (indexPath.section == 2){
        
        return 250;
        
    }else if(indexPath.section == 0){
        
        return 100;
    }
    
    return 0;
}

#pragma mark - headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return self.headerView;
    }
    
    return nil;
}

#pragma mark - 返回 headerView 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 120;
    }
    return 0;
}



#pragma mark - 出现的时候展示动画
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
    
        cell.transform = CGAffineTransformMakeTranslation(0, 40);
        
        [UIView animateWithDuration:(indexPath.item / 5.0) animations:^{
            
            cell.transform = CGAffineTransformIdentity;
            
        } completion:^(BOOL finished) {
            
        }];
    }
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, 120);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    if (_collectionView == nil) {
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    }
    
    _collectionView.bounces = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = YES;
    _collectionView.pagingEnabled = YES;
    
    _collectionView.backgroundColor = [UIColor greenColor];
    
    [_collectionView registerClass:[EGHomeAdCell class] forCellWithReuseIdentifier:@"homeAdCell"];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    return _collectionView;
}



#pragma mark -  tableView
- (UITableView *)newYearTableView{
    
    if (_newYearTableView == nil) {
        
        _newYearTableView = [[UITableView alloc]init];
        
        _newYearTableView.delegate = self;
        _newYearTableView.dataSource = self;
        
        _newYearTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_newYearTableView registerClass:[EGNewYearCell class] forCellReuseIdentifier:@"yearCell"];
        
        [_newYearTableView registerClass:[EGNewYearCell class] forCellReuseIdentifier:@"freshCell"];
        
        [_newYearTableView registerClass:[EGAdViewCell class] forCellReuseIdentifier:@"activitiesCell"];
    }
    return _newYearTableView;
}



- (UIView *)headerView{
    
    if (_headerView == nil) {
        _headerView = [[UIView alloc]init];
        
        _headerView.backgroundColor = [UIColor yellowColor];
    }
    return _headerView;
}


- (UIPageControl *)pageControl{
    
    if (_pageControl == nil) {
        
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
        
        _pageControl.numberOfPages = 5;
    }
    
    return _pageControl;
}


#pragma mark - 显示tabBar
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
}




@end
